package base

import (
	"context"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/common/errcode"
	roleRepo "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/repository/base/role"
	basePb "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/base"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/service/base/role"
	"git.myscrm.cn/golang/common/ykerrcode"
	"git.myscrm.cn/golang/stark/v4"
	"strings"
	"time"
)

type baseRoleServer struct {
	roleService role.RoleServiceIface
}

func NewBaseRoleServer(roleService role.RoleServiceIface) basePb.BaseRoleServiceServer {
	return &baseRoleServer{
		roleService: roleService,
	}
}

// RoleSave 修改/增加角色
func (s *baseRoleServer) RoleSave(ctx context.Context, req *basePb.RoleSaveRequest) (*basePb.RoleSaveResponse, error) {
	response := &basePb.RoleSaveResponse{}
	if req.Param == nil {
		return response, ykerrcode.TogRPCError(ykerrcode.INVALID_PARAMS, "角色名称为空")
	}

	param := req.Param
	param.RoleName = strings.Trim(param.RoleName, " ")
	if param.RoleName == "" {
		return response, ykerrcode.TogRPCError(ykerrcode.INVALID_PARAMS, "角色名称为空")
	}

	roleObj := &roleRepo.GfyRolePo{
		Id:         param.Id,
		Name:       param.RoleName,
		CreatedOn:  time.Now(),
		CreatedBy:  "",
		ModifiedOn: time.Now(),
		ModifiedBy: "",
	}

	roleId, err := s.roleService.RoleSave(ctx, roleObj)
	if err != nil {
		stark.Logger.Infof(ctx, "OrgSave-error: %+v, roleObj: %+v", err, roleObj)
		return response, errcode.CTogRPCError(err)
	}

	response.Id = roleId

	return response, nil
}

// DeleteRole 删除角色
func (s *baseRoleServer) DeleteRole(ctx context.Context, req *basePb.DeleteRoleRequest) (*basePb.DeleteRoleResponse, error) {
	response := &basePb.DeleteRoleResponse{}
	if req.Id == 0 {
		return response, ykerrcode.TogRPCError(ykerrcode.INVALID_PARAMS, "角色编号为空")
	}

	roleId, err := s.roleService.DeleteRole(ctx, req.Id, 0)
	if err != nil {
		stark.Logger.Infof(ctx, "DeleteRole-error: %+v, req.Id: %d", err, req.Id)
		return response, errcode.CTogRPCError(err)
	}

	response.Id = roleId
	return response, nil
}

// RoleList 角色列表
func (s *baseRoleServer) RoleList(ctx context.Context, req *basePb.RoleListRequest) (*basePb.RoleListResponse, error) {
	response := &basePb.RoleListResponse{}
	roleList, err := s.roleService.RoleList(ctx)
	if err != nil {
		stark.Logger.Infof(ctx, "RoleList-error: %+v", err)
		return response, err
	}

	response.List = make([]*basePb.RoleItem, 0)
	for _, item := range roleList {
		response.List = append(response.List, &basePb.RoleItem{
			Id:       item.Id,
			RoleName: item.Name,
		})
	}

	return response, nil
}

// RolePermissionList 根据角色获取权限列表
func (s *baseRoleServer) RolePermissionList(ctx context.Context, req *basePb.RolePermissionListRequest) (*basePb.RolePermissionListResponse, error) {
	response := &basePb.RolePermissionListResponse{}
	if req.RoleId == 0 {
		return response, ykerrcode.TogRPCError(ykerrcode.INVALID_PARAMS, "角色编号为空")
	}

	permissionList, err := s.roleService.RolePermissionList(ctx, req.RoleId)
	if err != nil {
		stark.Logger.Infof(ctx, "RolePermissionList-error: %+v, req.Id: %d", err, req.RoleId)
	}

	response.List = make([]*basePb.PermissionItem, 0)
	for _, item := range permissionList {
		response.List = append(response.List, &basePb.PermissionItem{
			Id:     item.Id,
			Name:   item.PerName,
			Type:   item.PerType,
			MenuId: item.GfyxMenuId,
		})
	}

	return response, nil
}

// AddPermissionsToRole 给角色授权
func (s *baseRoleServer) AddPermissionsToRole(ctx context.Context, req *basePb.AddPermissionsToRoleRequest) (*basePb.AddPermissionsToRoleResponse, error) {
	response := &basePb.AddPermissionsToRoleResponse{}
	if (req.RoleId == 0) || (len(req.PerIds) == 0) {
		return response, ykerrcode.TogRPCError(ykerrcode.INVALID_PARAMS, "角色编号为空")
	}

	err := s.roleService.AddPermissionsToRole(ctx, req.RoleId, req.PerIds)
	if err != nil {
		stark.Logger.Infof(ctx, "AddPermissionsToRole-info, err: %+v, req: %+v", err, req)
		return response, errcode.CTogRPCError(err)
	}

	response.IsSuccess = true

	return response, nil
}
